const { resolve } = require("path/posix");

class Request{
    constructor(options){
        this.method = options.method || "GET";
        this.host = options.host;
        this.port = options.port || 80;
        this.path = options.path || "/";
        this.body = options.body || {};
        this.headers = options.headers || {};
        if(!this.headers["Content-Type"]){
            this.headers["Content-Type"] = "application/x-www-form-urlencoded";
        
        }
        if(this.headers["Content-Type"] === "apllication/json"){
            this.bodyText = JSON.stringify(this.body);
        }else if(this.headers["Content-Type"] === "application/x-www-form-urlencoded"){
            this.bodyText = Object.keys(this.body).
                map(key => `${key}=${encodeURIComponent(this.body[key])}`).join('&');
        }

        this.headers["Content-Length"] = this.bodyText.length;
    }

    send(){
        return new Promise((resolve,reject) =>{
            // ...
        });
    }
}


void async function (){
    let req = new Request({
        method: "POST",
        host: "127.0.0.1",
        port:"9002",
        path: "/",
        headers: {
            ["x-Foo2"]:"customed"
        },
        body: {
            name:"ck",
            gender:"male"
        }
    });
    let res = await req.send();
    console.log(res);
}